﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Threading.Tasks;
using System.Drawing;
using Microsoft.Xna.Framework.Graphics;

using Color = System.Drawing.Color;


namespace MaterialEditor
{
    public class NodeConnector
    {
        public Node Start;
        private Node _end;
        public Node End
        {
            get { return _end; }
            set
            {
                if (value != null)
                    UpdateLine();

                _end = value;
            }
        }

        private bool _placed;
        public bool placed
        {
            get { return _placed; }

            set
            {
                if (value)
                    UpdateLine();

                _placed = value;
            }
        }

        private void UpdateLine()
        {
            linePoints = new Vector2[(int)Length / linespace];
            for (int i = 0; i < Length && i < linePoints.Length; i += linespace)
                linePoints[i] = Vector2.Hermite(startPoint, new Vector2(Length, 0), endPoint, new Vector2(Length, 0), (float)i / Length);
        }

        private Vector2[] linePoints;

        public Vector2 endPoint_unplaced;

        public Vector2 startPoint
        {
            get 
            {
                Microsoft.Xna.Framework.Vector2 v = startSlot.position;
                return new Vector2(v.X, v.Y);
            }
        }
        public Vector2 endPoint
        {
            get
            {
                if (placed)
                {
                    Microsoft.Xna.Framework.Vector2 v = End.GetInputLocation(endSlot);
                    return new Vector2(v.X, v.Y);
                }

                return endPoint_unplaced;
            }
        } 

        public InputSlot startSlot;
        public InputSlot endSlot;

        private float Length
        {
            get { return (int)Math.Abs(endPoint.X - startPoint.X); }
        }

        public NodeConnector(InputSlot startSlot)
        {
            this.startSlot = startSlot;

            Start = startSlot.parent;
            endPoint_unplaced = Start.position;
        }

        private const int CurveBoxDim = 50;

        public void Disconnect()
        {
            if (Start != null)
            {
                Start.output.connector = null;
                Start = null;
            }
            if (End != null)
            {
                foreach (InputSlot i in End.input)
                {
                    if (i == endSlot)
                    {
                        i.connector = null;
                        break;
                    }                        
                }
                End = null;
            }
        }

        private const int linespace = 9;
        public void Draw()
        {
            Graphics graphics = Graphics.Instance;

            Color c = startSlot.color;
            for (int i = 0; i < Length; i += linespace)
            {
                Vector2 p1 = Vector2.Hermite(startPoint, new Vector2(Length, 0), endPoint, new Vector2(Length, 0), (float)i / Length);
                Vector2 p2 = Vector2.Hermite(startPoint, new Vector2(Length, 0), endPoint, new Vector2(Length, 0), (float)(i + linespace) / Length);

                graphics.DrawLine(p1, p2, startSlot.color);
            }        
        }
    }
}
